
// Copyright (C) 2011 Luca Piccioni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//  
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Windows.Forms;

namespace Derm.Render.Surfaces
{
	/// <summary>
	/// 
	/// </summary>
	public partial class RenderControl : UserControl
	{
		/// <summary>
		/// 
		/// </summary>
		public RenderControl()
		{
			// No need to erase window background
			SetStyle(ControlStyles.AllPaintingInWmPaint, true);
			// No need to draw window background
			SetStyle(ControlStyles.Opaque, true);
			// Buffer control
			SetStyle(ControlStyles.DoubleBuffer, true);
			// Redraw window on resize
			SetStyle(ControlStyles.ResizeRedraw, true);
			// Painting handled by user
			SetStyle(ControlStyles.UserPaint, true);

			InitializeComponent();
		}

		#region Control Rendering

		/// <summary>
		/// Render on this UserControl.
		/// </summary>
		/// <param name="ctx">
		/// A <see cref="RenderContext"/> used for rendering this UserControl.
		/// </param>
		protected virtual void RenderThis(RenderContext ctx)
		{
		
		}

		/// <summary>
		/// A RenderWindow (bound to this UserControl) used for instancing a render context.
		/// </summary>
		private RenderWindow mRenderWindow;

		/// <summary>
		/// The render context used for rendering on this UserControl.
		/// </summary>
		private RenderContext mRenderContext;

		#endregion

		#region UserControl Overrides

		/// <summary>
		/// Raises the <see cref="E:System.Windows.Forms.Control.HandleCreated"/> event.
		/// </summary>
		/// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data. </param>
		protected override void OnHandleCreated(EventArgs e)
		{
			// Create the render window
			mRenderWindow = new RenderWindow(this);
			mRenderWindow.Create(null);
			// Create the renderer context
			mRenderContext = new RenderContext(mRenderWindow.GetDeviceContext());
			// Base implementation
			base.OnHandleCreated(e);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnHandleDestroyed(EventArgs e)
		{
			// Dispose the renderer context
			mRenderContext.Dispose();
			mRenderContext = null;
			// Dispose the renderer window
			mRenderWindow.Dispose();
			mRenderWindow = null;
			// Base implementation
			base.OnHandleDestroyed(e);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnPaint(PaintEventArgs e)
		{
			// Base implementation
			base.OnPaint(e);

			// Render the UserControl
			mRenderContext.MakeCurrent(true);
			RenderThis(mRenderContext);
			mRenderContext.MakeCurrent(false);
		}

		#endregion
	}
}
